<?php

namespace App\Models;

use App\Models\Rbac\User;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Employee extends BaseModel
{
    use HasFactory;
    protected $table = 'employee';
    // 允许批量操作的字段
    protected $fillable = ['employee_name','user_id','phone','address','is_del','job_type_id',
        'id_card',
        'native_place',
        'emergency_contact',
        'emergency_contact_phone',
        'card_number',
        'base_pay',
        'hiredate',
        'submit_way',
        'created_at','updated_at'
    ];


    public function scopeSearch($query,$params)
    {
        if(!empty($params['employee_name'])){
            $query->where('employee_name','like','%'.$params['employee_name'].'%');
        }
        if(!empty($params['card_number'])){
            $query->where('card_number',$params['card_number']);
        }
        if(!empty($params['job_type_id'])){
            $query->where('job_type_id',$params['job_type_id']);
        }

        $query->where('is_del',2);
        return $query;
    }

    public function job_type(){
        return $this->hasOne(JobType::class,'id','job_type_id');
    }

    public function user(){
        return $this->hasOne(User::class,'id','user_id')->with('role');
    }

    public function order_child(){
        return $this->hasMany(OrderChild::class,'employee_user_id','user_id');
    }
}
